package com.cater.project.controller;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.cater.annotation.SystemControllerLog;
import com.cater.common.PageResult;
import com.cater.project.entity.DemandCommunicate;
import com.cater.project.entity.DemandInterview;
import com.cater.project.service.DemandCommunicateServiceImpl;
import com.cater.project.service.DemandInterviewServiceImpl;
import com.cater.query.CommonQuery;

/**
 * 描述：面试记录控制层
 * 
 * @author yong
 * @date 2019/04/01
 */
@Controller
@RequestMapping("/demandInterview")
public class DemandInterviewController {

	@Autowired
	private DemandInterviewServiceImpl service;
	@Autowired
	private DemandCommunicateServiceImpl communicateService;

	/**
	 * @面试记录管理列表视图
	 * @return
	 */
	@RequestMapping("/list")
	public String list(CommonQuery query, Model model) {
		String communicateId = query.getCommunicateId();
		if (StringUtils.isNotEmpty(communicateId)) {
			model.addAttribute("communicateId", communicateId);
		}
		String recommendId = query.getRecommendId();
		if (StringUtils.isNotEmpty(recommendId)) {
			model.addAttribute("recommendId", recommendId);
		}
		return "project/demandInterview/list";
	}

	/**
	 * @面试记录管理数据分页
	 * @param query
	 * @param request
	 * @return
	 */
	@RequestMapping("listData")
	@ResponseBody
	@SystemControllerLog(description = "查询面试记录分页数据")
	public PageResult<DemandInterview> listData(CommonQuery query, HttpServletRequest request) {
		PageResult<DemandInterview> result = service.findPageResult(query);
		return result;
	}

	/**
	 * 添加视图
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "addView")
	public String addView(CommonQuery query, Model model) {
		String communicateId = query.getCommunicateId();
		if (StringUtils.isNotEmpty(communicateId)) {
			model.addAttribute("communicateId", communicateId);
		}
		String recommendId = query.getRecommendId();
		if (StringUtils.isNotEmpty(recommendId)) {
			model.addAttribute("recommendId", recommendId);
		}
		return "project/demandInterview/add";
	}

	/**
	 * 添加操作
	 * 
	 * @param t
	 * @return
	 */
	@RequestMapping(value = "add")
	@ResponseBody
	@SystemControllerLog(description = "添加面试记录")
	public JSONObject add(DemandInterview t, CommonQuery query) {
		JSONObject json = new JSONObject();
		String communicateId = query.getCommunicateId();
		if (StringUtils.isNotEmpty(communicateId)) {
			DemandCommunicate communicate = communicateService.getById(communicateId);
			if (communicate != null) {
				t.setTrueName(communicate.getTrueName());
			}
		}
		String id = service.saves(t);
		if (StringUtils.isNotBlank(id)) {
			json.put("code", 200);
			json.put("message", "success");
		} else {
			json.put("code", 999);
			json.put("message", "fail");
		}
		return json;
	}

	/**
	 * 修改视图
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "editView")
	public String editView(String id, Model model, CommonQuery query) {
		DemandInterview t = service.getById(id);
		model.addAttribute("demandInterview", t);
		return "project/demandInterview/update";
	}

	/**
	 * 修改操作
	 * 
	 * @param t
	 * @return
	 */
	@RequestMapping(value = "update")
	@ResponseBody
	@SystemControllerLog(description = "修改面试记录")
	public JSONObject update(DemandInterview t, CommonQuery query) {
		JSONObject json = new JSONObject();
		try {
			String id = t.getId();
			if (StringUtils.isNotEmpty(id)) {
				DemandInterview interview = service.getById(id);
				if (interview != null) {
					interview.setStatus("1");
					interview.setReason(t.getReason());
					service.update(interview);
					json.put("code", 200);
					json.put("message", "修改成功");
				}
			} else {
				json.put("code", 400);
				json.put("message", "数据不存在");
			}
		} catch (Exception e) {
			e.printStackTrace();
			json.put("code", 999);
			json.put("message", "修改失败");
		}
		return json;
	}

	/**
	 * 删除操作
	 * 
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/delete")
	@SystemControllerLog(description = "删除面试记录")
	public JSONObject delete(String id) {
		JSONObject json = new JSONObject();
		try {
			service.delete(id);
			json.put("code", 200);
			json.put("message", "删除成功");
		} catch (Exception e) {
			e.printStackTrace();
			json.put("code", 999);
			json.put("message", "删除失败");
		}
		return json;
	}

	/**
	 * 查看视图
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "detailView")
	public String detailView(String id, Model model, CommonQuery query) {
		DemandInterview t = service.getById(id);
		model.addAttribute("demandInterview", t);
		return "project/demandInterview/detail";
	}

	/**
	 * 修改面试反馈及状态
	 * 
	 * @param t
	 * @return
	 */
	@RequestMapping(value = "updateStatus")
	@ResponseBody
	@SystemControllerLog(description = "修改面试记录")
	public JSONObject updateStatus(DemandInterview t, CommonQuery query) {
		JSONObject json = new JSONObject();
		try {
			String id = t.getId();
			if (StringUtils.isNotEmpty(id)) {
				DemandInterview demandInterview = service.getById(id);
				if (demandInterview != null) {
					demandInterview.setStatus(t.getStatus());
					demandInterview.setReason(t.getReason());
					service.update(demandInterview);
					json.put("code", 200);
					json.put("message", "修改成功");
				}
			} else {
				String communicateId = t.getCommunicateId();
				if (StringUtils.isNotEmpty(communicateId)) {
					DemandCommunicate communicate = communicateService.getById(communicateId);
					if (communicate != null) {
						communicate.setIsPast(2);
						communicateService.update(communicate);
					}
				}
				service.saves(t);
				json.put("code", 200);
				json.put("message", "面试反馈成功");
			}
		} catch (Exception e) {
			e.printStackTrace();
			json.put("code", 999);
			json.put("message", "修改失败");
		}
		return json;
	}
}